﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Reflection;
using eCommerce.db.DataRespository;
using eCommerce.db.SQLDataAccess;

namespace eCommerce.db.DALRespository
{
    public class AdvDal
    {
        #region AdvDal private
        private static void SetInfo(IDataRecord reader, ref AdvEntity o)
        {
            for (int i = 0; i < reader.FieldCount; i++)
            {
                PropertyInfo propertyInfo = DbCommon.GetPropertyInfo(o, reader.GetName(i));
                if (((propertyInfo != null) && (reader[i] != null)) && (reader[i] != DBNull.Value))
                    propertyInfo.SetValue(o, reader[i], null);
                else
                    o[reader.GetName(i)] = reader[i];
            }
        }

        private static void SetInfoList(ref SqlDataReader reader, ref List<AdvEntity> advList)
        {
            while (reader.Read())
            {
                var o = new AdvEntity();
                SetInfo(reader, ref o);
                advList.Add(o);
            }
            reader.Dispose();
        }
        #endregion

        public static List<AdvEntity> GetListByTypeId(int typeId)
        {
            DbConnection.SetConnection(DbConnection.ConnectionName.Main);
            var lstAdv = new List<AdvEntity>();
            try
            {
                var reader = SqlHelper.ExecuteReader(DbCommon.ConnectionString, DbCommon.DatabaseSchema + "Adv_GetListByTypeId", typeId);
                SetInfoList(ref reader, ref lstAdv);
                return lstAdv;
            }
            catch (Exception ex)
            {
                throw new Exception(string.Format(DbCommon.DatabaseSchema + "Adv_GetListByTypeId: {0}", ex.Message));
            }
        }

        public static AdvEntity GetById(int id)
        {
            DbConnection.SetConnection(DbConnection.ConnectionName.Main);
            var o = new AdvEntity();
            try
            {
                var reader = SqlHelper.ExecuteReader(DbCommon.ConnectionString, DbCommon.DatabaseSchema + "Adv_GetById", id);
                if (reader.Read())
                    SetInfo(reader, ref o);
                return o;
            }
            catch (Exception ex)
            {
                throw new Exception(string.Format(DbCommon.DatabaseSchema + "Adv_GetById: {0}", ex.Message));
            }
        } 

        public static void Create(AdvEntity o)
        {
            DbConnection.SetConnection(DbConnection.ConnectionName.Main);
            try
            {
                SqlHelper.ExecuteNonQuery(DbCommon.ConnectionString, DbCommon.DatabaseSchema + "Adv_Create", o.TypeId, o.Priority, o.ImagePath, o.Url);
            }
            catch (Exception ex)
            {
                throw new Exception(string.Format(DbCommon.DatabaseSchema + "Adv_Create: {0}", ex.Message));
            }
        }

        public static void UpdateUrl(int id, string url)
        {
            DbConnection.SetConnection(DbConnection.ConnectionName.Main);
            try
            {
                SqlHelper.ExecuteNonQuery(DbCommon.ConnectionString, DbCommon.DatabaseSchema + "Adv_Update_Url", id, url);
            }
            catch (Exception ex)
            {
                throw new Exception(string.Format(DbCommon.DatabaseSchema + "Adv_Update_Url: {0}", ex.Message));
            }
        }

        public static void Delete(int advId)
        {
            DbConnection.SetConnection(DbConnection.ConnectionName.Main);
            try
            {
                SqlHelper.ExecuteNonQuery(DbCommon.ConnectionString, DbCommon.DatabaseSchema + "Adv_Delete", advId);
            }
            catch (Exception ex)
            {
                throw new Exception(string.Format(DbCommon.DatabaseSchema + "Adv_Delete: {0}", ex.Message));
            }
        }

        public static void DeleteByType(int typeId)
        {
            DbConnection.SetConnection(DbConnection.ConnectionName.Main);
            try
            {
                SqlHelper.ExecuteNonQuery(DbCommon.ConnectionString, DbCommon.DatabaseSchema + "Adv_DeleteByType", typeId);
            }
            catch (Exception ex)
            {
                throw new Exception(string.Format(DbCommon.DatabaseSchema + "Adv_DeleteByType: {0}", ex.Message));
            }
        }              
    }
}
